import { Storage } from './Storage';

export interface TEnvConfig {
  api: string;
}

interface TEnvConfigSet {
  [key: string]: TEnvConfig;
}

/**
 * 与环境相关的配置都在此
 */
const config: TEnvConfigSet = {
  dev: {
    api: 'https://apidev.stanly.link',
  },
  alpha: {
    api: 'https://apialpha.stanly.link',
  },
  beta: {
    api: 'https://apibeta.stanly.link',
  },
  prodGreen: {
    api: 'https://greenapi.stanly.link',
  },
  prod: {
    api: 'https://api.stanly.link',
  },
};

/**
 * 与环境相关的东西都在此获取或设置
 */
export namespace Env {
  let _env: string;
  let _nativeEnv: string;
  /** Version 1.4 是否是 HOME PAGE 2.0 */
  export const homePageV2: boolean = true;
  export async function init(anEnv: string) {
    if (!config[anEnv]) {
      throw new Error(`env.init: env ${anEnv} is not configed!`);
    }
    _nativeEnv = anEnv;
    if (__DEV__) {
      _env = Storage.get('modules.env', true);
    }
    if (!_env) _env = anEnv;
  }
  export function get() {
    if (!_env) {
      throw new Error(`env.get: env is not configed!`);
    }
    return {
      ...config[_env],
      env: _env,
      nativeEnv: _nativeEnv,
    };
  }

  export async function wget(_: number = 25) {
    return {
      ...config[_env],
      env: _env,
      nativeEnv: _nativeEnv,
    };
  }

  export function set(anEnv: string) {
    _env = anEnv;
    Storage.set('modules.env', anEnv, true);
  }
}
